home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / BasicInternalFrameTitlePane.java < prev    next >
Text File  |  1998-06-30  |  12KB  |  339 lines

  1. /*
  2.  * @(#)BasicInternalFrameTitlePane.java    1.16 98/04/10
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.basic;
  22.  
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import com.sun.java.swing.*;
  26. import com.sun.java.swing.border.*;
  27. import com.sun.java.swing.event.InternalFrameEvent;
  28. import java.util.EventListener;
  29. import java.beans.PropertyChangeListener;
  30. import java.beans.PropertyChangeEvent;
  31. import java.beans.VetoableChangeListener;
  32. import java.beans.PropertyVetoException;
  33.  
  34. /**
  35.  * The class that manages a basic title bar
  36.  * <p>
  37.  * Warning: serialized objects of this class will not be compatible with
  38.  * future swing releases.  The current serialization support is appropriate
  39.  * for short term storage or RMI between Swing1.0 applications.  It will
  40.  * not be possible to load serialized Swing1.0 objects with future releases
  41.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  42.  * baseline for the serialized form of Swing objects.
  43.  *
  44.  * @version 1.16 04/10/98
  45.  * @author David Kloba
  46.  */
  47. public class BasicInternalFrameTitlePane 
  48.     extends JComponent implements LayoutManager, ActionListener, PropertyChangeListener 
  49. {
  50.     JMenuBar menuBar;
  51.     JButton iconButton;
  52.     JButton maxButton;
  53.     JButton closeButton;
  54.     Icon maxIcon = UIManager.getIcon("InternalFrame.maximizeIcon");
  55.     Icon minIcon = UIManager.getIcon("InternalFrame.minimizeIcon");
  56.     Icon iconIcon = UIManager.getIcon("InternalFrame.iconifyIcon");
  57.     Icon closeIcon = UIManager.getIcon("InternalFrame.closeIcon");
  58.     JMenu windowMenu;
  59.     JInternalFrame frame;
  60.  
  61.     private static Color selectedTitleColor = new Color(0, 0, 128);
  62.     private static Color selectedTextColor = new Color(255, 255, 255);
  63.     private static Color notSelectedTitleColor = new Color(128, 128, 128);
  64.     private static Color notSelectedTextColor = new Color(192, 192, 192);
  65.  
  66.     
  67.     final int RESTORE_MENU_ITEM = 0;
  68.     final int MOVE_MENU_ITEM = 1;
  69.     final int SIZE_MENU_ITEM = 2;
  70.     final int MINIMIZE_MENU_ITEM = 3;
  71.     final int MAXIMIZE_MENU_ITEM = 4;
  72.     final int SEPARATOR_MENU_ITEM = 5;
  73.     final int CLOSE_MENU_ITEM = 6;
  74.     
  75.     private class NoFocusButton extends JButton {
  76.     public boolean isFocusTraversable() { return false; }
  77.     public void requestFocus() {};
  78.         public boolean isOpaque() { return true; }
  79.     };
  80.  
  81.     public BasicInternalFrameTitlePane(JInternalFrame f) {
  82.     JMenuItem mi;
  83.  
  84.     frame = f;
  85.     
  86.     setPreferredSize(new Dimension(100, 18));
  87.  
  88.     menuBar = new JMenuBar(){
  89.         public boolean isFocusTraversable() { return false; }
  90.         public void requestFocus() {}
  91.         public void paint(Graphics g) {
  92.                 Icon icon = frame.getFrameIcon();
  93.                 if (icon == null) {
  94.                     icon = UIManager.getIcon("InternalFrame.icon");
  95.                 }
  96.                 if (icon != null) {
  97.                     // Resize to 16x16 if necessary.
  98.                     if (icon instanceof ImageIcon &&
  99.                         (icon.getIconWidth() > 16 || 
  100.                          icon.getIconHeight() > 16)) {
  101.                         Image img = ((ImageIcon)icon).getImage();
  102.                         ((ImageIcon)icon).setImage(
  103.                             img.getScaledInstance(16, 16, Image.SCALE_SMOOTH));
  104.                     }
  105.                     icon.paintIcon(this, g, 0, 0);
  106.                 }
  107.             }
  108.         public boolean isOpaque() { return true; }
  109.     };
  110.     
  111.     menuBar.setBorderPainted(false);
  112.     windowMenu = (JMenu) menuBar.add(new JMenu("    ") {
  113.         boolean neverBeenSet = true;
  114.         public void setPopupMenuVisible(boolean b) {
  115.         if(neverBeenSet) {
  116.             if(!frame.isIconifiable())
  117.             windowMenu.getItem(MINIMIZE_MENU_ITEM).setEnabled(false);
  118.             if(!frame.isMaximizable()) 
  119.             windowMenu.getItem(MAXIMIZE_MENU_ITEM).setEnabled(false);
  120.             if(!frame.isMaximizable() && !frame.isIconifiable()) 
  121.             windowMenu.getItem(RESTORE_MENU_ITEM).setEnabled(false);
  122.             if(!frame.isClosable())    
  123.             windowMenu.getItem(CLOSE_MENU_ITEM).setEnabled(false);            
  124.             neverBeenSet = false;
  125.         }
  126.         super.setPopupMenuVisible(b);
  127.         }                  
  128.     });
  129.     
  130.     mi = (JMenuItem) windowMenu.add(new JMenuItem("Restore"));
  131.     mi.setEnabled(false);
  132.     mi.addActionListener(this);
  133.     /// PENDING(klobad) Move/Size actions on InternalFrame need to be determined
  134.     mi = (JMenuItem) windowMenu.add(new JMenuItem("Move"));
  135.     mi.setEnabled(false);
  136.     mi.addActionListener(this);
  137.     mi = (JMenuItem) windowMenu.add(new JMenuItem("Size"));
  138.     mi.setEnabled(false);
  139.     mi.addActionListener(this);
  140.     mi = (JMenuItem) windowMenu.add(new JMenuItem("Minimize"));
  141.     mi.addActionListener(this);
  142.     mi = (JMenuItem) windowMenu.add(new JMenuItem("Maximize"));
  143.     mi.addActionListener(this);
  144.     windowMenu.add(new JSeparator());
  145.     mi = (JMenuItem) windowMenu.add(new JMenuItem("Close"));
  146.     mi.addActionListener(this); 
  147.     
  148.     iconButton = new NoFocusButton();
  149.     iconButton.setFocusPainted(false);
  150.     iconButton.addActionListener(this);
  151.     iconButton.setActionCommand("Iconify");
  152.  
  153.     maxButton = new NoFocusButton();
  154.     maxButton.setFocusPainted(false);
  155.     maxButton.addActionListener(this);
  156.     maxButton.setActionCommand("Maximize");
  157.  
  158.         setButtonIcons();
  159.  
  160.     closeButton = new NoFocusButton();
  161.     closeButton.setIcon(closeIcon);
  162.     closeButton.setFocusPainted(false);     
  163.     closeButton.addActionListener(this);
  164.     closeButton.setActionCommand("Close");
  165.             
  166.     setLayout(this);
  167.  
  168.     add(menuBar);
  169.     add(iconButton);
  170.     add(maxButton);
  171.     add(closeButton);             
  172.  
  173.     // Make sure these are ok to leave on?
  174.     frame.addPropertyChangeListener(this);
  175.     }
  176.  
  177.     protected void setButtonIcons() {
  178.     if(frame.isIcon()) {
  179.         iconButton.setIcon(minIcon);
  180.         maxButton.setIcon(maxIcon);
  181.         } else if (frame.isMaximum()) {
  182.         iconButton.setIcon(iconIcon);
  183.         maxButton.setIcon(minIcon);
  184.         } else {
  185.         iconButton.setIcon(iconIcon);
  186.         maxButton.setIcon(maxIcon);
  187.         }
  188.     }
  189.         
  190.     public void paint(Graphics g)  {
  191.     boolean isSelected = frame.isSelected();
  192.     Color color = g.getColor();
  193.  
  194.     if(isSelected)
  195.         g.setColor(selectedTitleColor);
  196.     else
  197.         g.setColor(notSelectedTitleColor);
  198.     g.fillRect(0, 0, getWidth(), getHeight());
  199.  
  200.     if(frame.getTitle() != null) {
  201.         Font f = g.getFont();
  202.         g.setFont(UIManager.getFont("InternalFrame.titleFont"));
  203.         if(isSelected)
  204.         g.setColor(selectedTextColor);
  205.         else
  206.         g.setColor(notSelectedTextColor);
  207.  
  208.             // Center text vertically.
  209.         FontMetrics fm = g.getFontMetrics();
  210.             int fmHeight = fm.getHeight() - fm.getLeading();
  211.             int baseline = (18 - fmHeight) / 2 + 
  212.                 fm.getAscent() + fm.getLeading();
  213.             
  214.         g.drawString(frame.getTitle(), 
  215.                          menuBar.getX() + menuBar.getWidth() + 2, 
  216.                          baseline);
  217.         g.setFont(f);
  218.     }
  219.             
  220.     g.setColor(color);
  221.     super.paint(g);
  222.     }
  223.  
  224.     /**
  225.      * Post a WINDOW_CLOSING-like event to the frame, so that it can
  226.      * be treated like a regular Frame.
  227.      */
  228.     void postClosingEvent(JInternalFrame frame) {
  229.         InternalFrameEvent e = new InternalFrameEvent(
  230.             frame, InternalFrameEvent.INTERNAL_FRAME_CLOSING);
  231.         // Try posting event, unless there's a SecurityManager.
  232.         if (JInternalFrame.class.getClassLoader() == null) {
  233.             try {
  234.                 Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(e);
  235.                 return;
  236.             } catch (SecurityException se) {
  237.                 // Use dispatchEvent instead.
  238.             }
  239.         }
  240.         frame.dispatchEvent(e);
  241.     }
  242.  
  243.     public void actionPerformed(ActionEvent e) {
  244.     if("Close".equals(e.getActionCommand()) && frame.isClosable())
  245.             postClosingEvent(frame);
  246.     else if("Iconify".equals(e.getActionCommand()) && frame.isIconifiable()) {
  247.         if(!frame.isIcon())
  248.         try { frame.setIcon(true); } catch (PropertyVetoException e1) { }
  249.         else {
  250.         try { 
  251.                     frame.setIcon(false); 
  252.                     if (frame.isMaximizable() && frame.isMaximum()) {
  253.                         frame.setMaximum(false);
  254.                     }
  255.                 } catch (PropertyVetoException e1) { }
  256.             }
  257.     } else if("Minimize".equals(e.getActionCommand()) && frame.isIconifiable()) {
  258.         try { frame.setIcon(true); } catch (PropertyVetoException e2) { }
  259.     } else if("Maximize".equals(e.getActionCommand()) && frame.isMaximizable()) {
  260.         if(!frame.isMaximum()) {
  261.         try { frame.setMaximum(true); } catch (PropertyVetoException e5) { }
  262.         } else {
  263.         try { 
  264.                     frame.setMaximum(false); 
  265.                     if (frame.isIconifiable() && frame.isIcon()) {
  266.                         frame.setIcon(false); 
  267.                     }
  268.                 } catch (PropertyVetoException e6) { }
  269.         }
  270.     } else if("Restore".equals(e.getActionCommand()) && 
  271.                   frame.isMaximizable() && frame.isMaximum()) {
  272.         try { frame.setMaximum(false); } catch (PropertyVetoException e4) { }
  273.     } else if("Restore".equals(e.getActionCommand()) && 
  274.                   frame.isIconifiable() && frame.isIcon()) {
  275.         try { frame.setIcon(false); } catch (PropertyVetoException e4) { }
  276.     }
  277.     }
  278.  
  279.     public void propertyChange(PropertyChangeEvent evt) {
  280.     String prop = (String)evt.getPropertyName();
  281.     JInternalFrame f = (JInternalFrame)evt.getSource();
  282.     boolean value = false;
  283.     // ASSERT(frame == f) - This should always be true
  284.     if(JInternalFrame.IS_SELECTED_PROPERTY.equals(prop)) {
  285.         //            value = ((Boolean)evt.getNewValue()).booleanValue();
  286.         //        if(!value)
  287.         repaint();
  288.     } else if(JInternalFrame.IS_MAXIMUM_PROPERTY.equals(prop)) {
  289.         value = ((Boolean)evt.getNewValue()).booleanValue();
  290.             setButtonIcons();
  291.         windowMenu.getItem(RESTORE_MENU_ITEM).setEnabled(value); 
  292.         windowMenu.getItem(MAXIMIZE_MENU_ITEM).setEnabled(!value); 
  293.     } else if(JInternalFrame.IS_ICON_PROPERTY.equals(prop)) {
  294.         value = ((Boolean)evt.getNewValue()).booleanValue();
  295.             setButtonIcons();
  296.         windowMenu.getItem(RESTORE_MENU_ITEM).setEnabled(value); 
  297.         windowMenu.getItem(MAXIMIZE_MENU_ITEM).setEnabled(!value); 
  298.         windowMenu.getItem(MINIMIZE_MENU_ITEM).setEnabled(!value); 
  299.     } 
  300.     }
  301.  
  302.     public void addLayoutComponent(String name, Component c) {}
  303.     public void removeLayoutComponent(Component c) {}    
  304.     public Dimension preferredLayoutSize(Container c)  {
  305.     return new Dimension(c.getSize().width, 18);
  306.     }
  307.     
  308.     public Dimension minimumLayoutSize(Container c) {
  309.     return new Dimension(c.getSize().width, 18);
  310.     }
  311.     
  312.     public void layoutContainer(Container c) {
  313.     int w = getWidth();
  314.     int x = w - 16 - 2;
  315.     menuBar.setBounds(2, 1, 16, 16);
  316.  
  317.     if(frame.isClosable()) {
  318.         closeButton.setBounds(x, 2, 16, 14);
  319.         x -= 16;
  320.     } else if(closeButton.getParent() != null) {
  321.         closeButton.getParent().remove(closeButton);
  322.     }
  323.             
  324.     if(frame.isMaximizable()) {
  325.         maxButton.setBounds(x - 2, 2, 16, 14);
  326.         x -= 18;
  327.     } else if(maxButton.getParent() != null) {
  328.         maxButton.getParent().remove(maxButton);
  329.     }
  330.         
  331.     if(frame.isIconifiable()) {
  332.         iconButton.setBounds(x, 2, 16, 14);
  333.     } else if(iconButton.getParent() != null) {
  334.         iconButton.getParent().remove(iconButton);
  335.     }
  336.     }
  337. };    /// End Title Pane Class
  338.  
  339.